package com.primary.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.primary.common.CommonStr;
import com.primary.controller.request.BloodbanksRequest;
import com.primary.entity.Bloodbanks;
import com.primary.mappers.BloodbanksMapper;
import com.primary.service.BloodbanksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service("BloodbanksService")
public class BloodbanksServiceImpl implements BloodbanksService {

    @Autowired
    BloodbanksMapper bloodbanksMapper;

    /**
     * 分页显示血液库存信息
     *
     * @param bloodbanksRequest
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> queryByPage(BloodbanksRequest bloodbanksRequest) {
        PageHelper.startPage(bloodbanksRequest.getPageNum(), bloodbanksRequest.getPageSize());
        return new PageInfo<>(bloodbanksMapper.listByPage(bloodbanksRequest));
    }

    /**
     * 插入一条数据
     *
     * @param bloodbanks
     * @return
     */
    @Override
    public int insertOne(Bloodbanks bloodbanks) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = sdf.format(bloodbanks.getInstoreTime());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String instoredate = sdf2.format(bloodbanks.getInstoreTime());
        // 拼接生成的信息，由“用户名”+下划线+日期信息
        bloodbanks.setBloodInfo(bloodbanks.getDonors()+ CommonStr.UNDERSCORE +datetime);
        bloodbanks.setInstoredate(instoredate);
        return bloodbanksMapper.insertOne(bloodbanks);
    }

    /**
     * 更新一条数据
     *
     * @param bloodbanks
     * @return
     */
    @Override
    public int updateOneById(Bloodbanks bloodbanks) {
        if(bloodbanks == null){
            return 0;
        }
        if(CommonStr.ZERO.equals(bloodbanks.getStatus())){
            return bloodbanksMapper.updateOneById(bloodbanks);
        } else if (CommonStr.ONE.equals(bloodbanks.getStatus())) {
            return bloodbanksMapper.updateOneByIdStatus(bloodbanks);
        }
        return 0;
    }

    /**
     * 删除一条记录
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        return bloodbanksMapper.deleteById(id) > 0;
    }

    /**
     * 方便前端选择血液信息
     *
     * @return
     */
    @Override
    public List<String> queryBloodbanksForSelecter() {
        return bloodbanksMapper.queryBloodbanksForSelecter();
    }

    /**
     * charts图表
     *
     * @return
     */
    @Override
    public List<Bloodbanks> queryAll() {
        return bloodbanksMapper.queryAll();
    }

    /**
     * echarts图表
     *
     * @return
     */
    @Override
    public List<Bloodbanks> queryAllForBarAndPie() {
        return bloodbanksMapper.queryAllForBarAndPie();
    }
}
